//
//  LoginViewController.m
//  OrderSeat
//
//  Created by haiming on 13-10-4.
//  Copyright (c) 2013年 wangwei. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "QuickRegisterViewController.h"
#import "ForgotPasswordViewController.h"
#import "LoggedViewController.h"
#import "ASIHTTPRequest.h"
#import "AppSetting.h"
#import "MeViewController.h"

#define LOGIN_TAG 1001
#define FOGOT_TAG  1002
#define FAST_REGISTER_TAG 1003

@implementation LoginViewController

- (id)init
{
    self = [super initWithNibClass:[LoginViewController class]];
    if (self) {
        _dataSource = [[LoginViewDataSource alloc] initWithVC:self];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupUI];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[AppDelegate sharedInstance] hideTabBar:self.tabBarController];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[AppDelegate sharedInstance] showTabBar:self.tabBarController];
}

- (void)setupUI
{
    [self createLeftButton];
    [self createNavigation:@"会员登录"];
    [self createRightButton:@"注册"];
    
    [_contentView drawBorder];
    [self.view addTarget:self action:@selector(viewTapped:)];
}

- (void)leftButtonClick:(id)sender
{
    if (_backController) {
        [self.navigationController popToViewController:_backController animated:YES];
    }else {
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
}

- (void)rightButtonClick:(id)sender
{
    [self pushOrPopViewController:[RegisterViewController class]];
}

- (IBAction)buttonAction:(id)sender
{
    UIButton* btn = (UIButton*)sender;
    
    if (btn.tag == LOGIN_TAG) {
        if (![self checkForm]) return;
        [self viewTapped:sender];
        
        _userName = _userNameTF.text;
        _password = _passwordTF.text;
        NSString *url = [NSString stringWithFormat:API_LOGIN,_userName,_password];
        [_dataSource loginWithURL:url];
    }
    
    if (btn.tag == FOGOT_TAG) {
        ForgotPasswordViewController *fogotVC = [[ForgotPasswordViewController alloc] initWithPassType:Forgot];
        [self pushViewController:fogotVC];
    }
    
    if (btn.tag == FAST_REGISTER_TAG) {
        QuickRegisterViewController *quickVC = [[QuickRegisterViewController alloc] initWithType:FastRegister];
        [self pushViewController:quickVC];
    }
}

- (void)clearForm
{
    _userNameTF.text = S_EMPTY;
    _passwordTF.text = S_EMPTY;
}

- (BOOL)checkForm
{
    if (_userNameTF.text.length <= 0) {
        [AppDelegate showMessage:@"用户名不能为空！"];
        return NO;
    }
    
    if (_passwordTF.text.length <= 0) {
        [AppDelegate showMessage:@"密码不能为空!"];
        return NO;
    }
    
    return YES;
}

-(void)viewTapped:(id)sender
{
    [_userNameTF resignFirstResponder];
    [_passwordTF resignFirstResponder];
}

@end

//=========================================================================
#pragma ===================数据源类实现======================================
//=========================================================================

@implementation LoginViewDataSource

- (id)initWithVC:(id)viewController
{
    self = [super init];
    if(self) {
        self.viewController = viewController;
    }
    return self;
}

- (void)loginWithURL:(NSString *)url
{
    [SVProgressHUD show];
    [self get:url];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    [SVProgressHUD dismiss];
    CLog(@"%@", request.responseString);
    
    NSDictionary *json = [request.responseData JSONValue];;
    NSDictionary *data = [json objectForKey:@"data"];
    
    [self processRequest:data];
}

- (void)processRequest:(NSDictionary *)data
{
    int state = [[data objectForKey:@"state"] integerValue];
    if (state == 1) {
        [AppSetting setUserName:[data objectForKey:@"huiyuan"]];
        
        LoggedViewController * loggedVC = [[LoggedViewController alloc] init];
        loggedVC.userName = _viewController.userName;
        loggedVC.backController = _viewController.backController;
        [_viewController pushViewController:loggedVC];
        
        [_viewController clearForm];
    }
    
    if (state == 2) {
        [AppDelegate showMessage:@"你所输入的帐号或密码错误!"];
    }
    
    if (state == 3) {
        [AppDelegate showMessage:@"你所输入的帐号或密码错误!"];
    }
    
    if (state == 4) {
        [SVProgressHUD showSuccessWithStatus:@"需验证手机号码" duration:1];
        
        QuickRegisterViewController *registerVC = [[QuickRegisterViewController alloc] initWithType:ValidateTel];
        
        [_viewController performSelector:@selector(pushViewController:) withObject:registerVC afterDelay:1.2];
    }
}

@end

